;; 写代码相关配置
(use-package yasnippet
  :config
  (yas-global-mode 1)
  (yas/load-directory "~/.emacs.d/snippets")
  (yas-reload-all)
  :hook
  ((prog-mode-hook . yas-minor-mode)
   (org-mode-hook . yas-minor-mode)))

;; Press M-(digit) to quickly complete with one of the first 10 candidates.
(use-package company
  :config
  (setq company-minimun-prefix-lenght 2
	company-show-numbers t
	company-tooltip-limit 20
	company-idle-delay 0
	company-echo-delay 0
	company-tooltip-offset-display 'scrollbar
	company-begin-commands '(self-insert-command))
  (push '(company-semantic :with company-yasnippet) company-backends)
  :bind (:map company-active-map
	      ("C-n" . 'company-select-next)
	      ("C-p" . 'company-select-previous)
	      ("C-f" . 'company-select-first)
	      ("C-l" . 'company-select-last))
  :hook ((after-init . global-company-mode)))
 
(with-eval-after-load 'company
  (defadvice company-in-string-or-comment (around company-in-string-or-comment-hack activate)
    (if (memq major-mode '(php-mode html-mode web-mode nxml-mode))
	ad-do-it)))

(use-package projectile :ensure t)

(use-package rust-mode)



(use-package go-mode
  :hook
  ((go-web-hook . (lambda () (setq indent-tabs-mode 1) (setq-local tab-width 4)))))

;; dockerfile
;; npm install -g dockerfile-language-server-nodejs
(use-package dockerfile-mode)
;; pip install 'python-language-server[all]'

;; haskell language
;; ghcup install hls
(use-package haskell-mode)
;; (use-package haskell-snippets)

;; clojure
;; sudo bash < <(curl -s https://raw.githubusercontent.com/clojure-lsp/clojure-lsp/master/install)

(use-package clojure-mode)

(use-package eglot
  :config
  (add-hook 'python-mode-hook 'eglot-ensure)
  (add-hook 'ruby-mode-hook 'eglot-ensure)
  (add-to-list 'eglot-server-programs '((c++-mode c-mode) "clangd"))
  (add-to-list 'eglot-server-programs '(rust-mode . ("rust-analyzer")))
  (add-hook 'c-mode-hook 'eglot-ensure)
  (add-hook 'c++-mode-hook 'eglot-ensure)
  (add-hook 'rust-mode-hook 'eglot-ensure)
  (add-hook 'go-mode-hook 'eglot-ensure)
  (add-hook 'dockerfile-mode-hook 'eglot-ensure)
  (add-hook 'haskell-mode-hook 'eglot-ensure)
  (add-hook 'clojure-mode-hook 'eglot-ensure)
)

;; sudo snap install --edge marksman
;; https://github.com/artempyanykh/marksman
(use-package markdown-mode)
(add-to-list 'eglot-server-programs '(markdown-mode . ("marksman")))
(add-hook 'markdown-mode-hook #'eglot-ensure)

;; web

;; html标签补全
(use-package emmet-mode
  :ensure t
  :commands emmet-mode
  :hook
  (sgml-mode . emmet-mode)
  (web-mode  . emmet-mode)
  (css-mode  . emmet-mode))

;; prettier
(use-package prettier-js
  :ensure t
  )

(defun format-with-prettier ()
  (interactive)
  (prettier-js)
  (princ "格式化好了，牛逼666")
  )

;; 格式化快捷键
(global-set-key (kbd "C-M-\\") 'format-with-prettier)

;; 优先读取node_modules的path
(use-package add-node-modules-path
  :ensure t
  :config
  ;; automatically run the function when web-mode starts
  (eval-after-load 'web-mode
    '(add-hook 'web-mode-hook 'add-node-modules-path)))

(use-package css-mode
  :ensure t
  :mode "\\.css\\'"
  :config
  (setq css-indent-offset 2)
  (add-hook 'scss-mode-hook #'electric-pair-mode)
  )


;; web-mode
(use-package web-mode
  :ensure t
  ;; :mode ("\\.html\\'" "\\.tsx\\'" "\\.js\\'" "\\.scss\\'" "\\.json\\'" "\\.jsx\\'")
  ;; :mode ("\\.vue\\'" "\\.html\\'" "\\.ts\\'" "\\.js\\'" "\\.json\\'" "\\.jsx\\'")
  :mode ("\\.vue\\'" "\\.html\\'" "\\.tsx\\'" "\\.ts\\'" "\\.js\\'" "\\.json\\'" "\\.jsx\\'")
  :bind(("C-,". lsp-find-definition)
        ("C-'". completion-at-point))
  :hook(
        (web-mode . electric-pair-mode));; 回车括号光标居中
  :config  
  (add-to-list 'web-mode-comment-formats '(("javascript" . "//")
                                           ("js" . "//")
                                           ;;("ts" . "//")
                                           ("tsx" . "//")
                                           ("vue" . "//"))))

(use-package docker-compose-mode)
(use-package cmake-mode)

(provide 'prog)
